package org.jwd.core.exception;

import lombok.extern.slf4j.Slf4j;
import org.jwd.core.web.Res;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author vizz
 * @since 2022/5/19 20:38
 * <p>
 * 全局异常捕获
 * 捕获后直接通过http返回给
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Res handler(IllegalArgumentException e) {
        log.error("Assert异常：----------------{}", e.getMessage());
        return Res.fail(500, e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RollBackWithNoIdException.class)
    public Res handler(RollBackWithNoIdException e) {
        log.error("回滚缺乏id：----------------{}", e.getMessage());
        return Res.fail(500, e.getMessage());
    }
}
